package cc.ufinity.networkdiagnose.view;

import java.text.SimpleDateFormat;
import java.util.Date;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.widget.Toast;
import cc.ufinity.networkdiagnose.app.GlobalApp;

public class SMSBroadcastReceiver extends BroadcastReceiver {
	
    public void onReceive(Context context, Intent intent) {
        SmsMessage msg = null;
            Bundle bundle = intent.getExtras();
            if (bundle != null) {
                Object[] pdusObj = (Object[]) bundle.get("pdus");
                for (Object p : pdusObj) {
                    msg= SmsMessage.createFromPdu((byte[]) p);
                    
                    String msgTxt =msg.getMessageBody();//�õ���Ϣ������
                    
                    Date date = new Date(msg.getTimestampMillis());//ʱ��
                    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    String receiveTime = format.format(date);
                    
                    String senderNumber = msg.getOriginatingAddress();
                System.out.println("----------------"+msgTxt);
                    if (msgTxt!=null&&msgTxt.contains("您的集团客户WLAN密码为")) {
        		        int index = msgTxt.indexOf("您的集团客户WLAN密码为");
        		        msgTxt = msgTxt.substring(index +13, index +19);
        		        System.out.println("-------------集团客户密码："+msgTxt);
        				GlobalApp.SmsBody = msgTxt;
        			}
                    
               /* if (msgTxt.equals("Testing!")) {
                    Toast.makeText(context, "success!", Toast.LENGTH_LONG)
                            .show();
                    System.out.println("success!");
                    return;
                } else {
                    Toast.makeText(context, msgTxt, Toast.LENGTH_LONG).show();
                    System.out.println("发送人："+senderNumber+"  内容："+msgTxt+"时间"+receiveTime);
                    return;
                }*/
            }
            return;
        }
    }
}